<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="链接类型" prop="type">
        <el-select v-model="formData.type" clearable placeholder="请选择" @change="Onselect">
          <el-option
            v-for="(item,index) in type"
            :key=" index"
            :label="item.name"
            :value="item.type"
          />

        </el-select>
      </el-form-item>
      <!-- 文字类型弹出来的弹框 -->
      <el-form-item v-if="titleShow" label="标题" proop="title">
        <el-input v-model="formData.title" type="text" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="链接地址" prop="url">
        <el-input
          v-model="formData.url"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="链接描述" prop="des">
        <el-input v-model="formData.des" placeholder="请输入" />
      </el-form-item>
      <el-form-item v-if="imgShow" label="链接图片">
        <el-upload
          class="upload-demo"
          action="#"
          :http-request="httpRequest"
          :file-list="fileList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addYddLinkInfoApi, updateYddLinkInfoApi } from '@/api/ydd_link'
import { UploadFile } from '@/api/user'
export default {
  name: 'AddFormLink',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        type: '',
        url: '',
        title: '',
        des: '',
        img: ''
      },
      titleShow: false,
      imgShow: false,
      type: [
        { type: '1', name: '文字类型' },
        { type: '2', name: '链接类型' }
      ],
      fileList: [],
      // options: [1, 2],
      // 表单验证规则
      formRules: {
        advposdesc: [{ required: true, message: '请输入', trigger: 'blur' }],
        title: [{ required: true, message: '请输入', trigger: 'blur' }],
        url: [{ required: true, message: '请输入', trigger: 'blur' }],
        des: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      if (this.formData.id) {
        await updateYddLinkInfoApi(this.formData)
      } else {
        await addYddLinkInfoApi(this.formData)
      }

      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        advposdesc: '',
        advposname: '',
        advpossize: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    },
    Onselect(type) {
      this.formData.type = type
      // console.log(type)
      if (type === '1') {
        this.titleShow = true
        this.imgShow = false
      } else {
        this.imgShow = true
        this.titleShow = false
      }
    },
    async httpRequest(val) {
      console.log(val)
      const fd = new FormData()
      fd.append('file', val.file)
      const res = await UploadFile(fd)
      console.log(res)
      this.formData.img = res.data.savePath
    }
  }
}
</script>
<style scoped></style>
